cat('Creating Tables A9 and A10 \n\n')


dat = read.csv('TablesA9A10_data.csv')
non.south.dat = dat[dat$south == 0,]


reg1 = lm(obama.vote~dissimilarity+percent.black,
        data = dat)
clustered.se = cl(dat, reg1, dat$cbsa_NAMELSAD10)
reg1$se = clustered.se

reg2 = lm(obama.vote~dissimilarity+percent.black+I(percent.black^2),
          data = dat)
clustered.se = cl(dat, reg2, dat$cbsa_NAMELSAD10)
reg2$se = clustered.se

reg3 = lm(obama.vote~dissimilarity*percent.black,
          data = dat)
clustered.se = cl(dat, reg3, dat$cbsa_NAMELSAD10)
reg3$se = clustered.se

reg4 = lm(obama.vote~dissimilarity*percent.black+dissimilarity*I(percent.black^2),
          data = dat)
clustered.se = cl(dat, reg4, dat$cbsa_NAMELSAD10)
reg4$se = clustered.se

reg5 = lm(obama.vote~dissimilarity+percent.black+income.recode+college.educated,
          data = dat)
clustered.se = cl(dat, reg5, dat$cbsa_NAMELSAD10)
reg5$se = clustered.se

reg6 = lm(obama.vote~dissimilarity+percent.black+I(percent.black^2)+income.recode+college.educated,
          data = dat)
clustered.se = cl(dat, reg6, dat$cbsa_NAMELSAD10)
reg6$se = clustered.se

reg7 = lm(obama.vote~dissimilarity*percent.black+income.recode+college.educated,
          data = dat)
clustered.se = cl(dat, reg7, dat$cbsa_NAMELSAD10)
reg7$se = clustered.se

reg8 = lm(obama.vote~dissimilarity*percent.black+dissimilarity*I(percent.black^2)+income.recode+college.educated,
          data = dat)
clustered.se = cl(dat, reg8, dat$cbsa_NAMELSAD10)
reg8$se = clustered.se

coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     Sweave = T,
                     notes = '',
                     stars = 'default'
    )
writeLines(
  outtable, 'TableA9.tex')




reg1 = lm(obama.vote~dissimilarity+percent.black,
          data = non.south.dat)
clustered.se = cl(non.south.dat, reg1, non.south.dat$cbsa_NAMELSAD10)
reg1$se = clustered.se

reg2 = lm(obama.vote~dissimilarity+percent.black+I(percent.black^2),
          data = non.south.dat)
clustered.se = cl(non.south.dat, reg2, non.south.dat$cbsa_NAMELSAD10)
reg2$se = clustered.se

reg3 = lm(obama.vote~dissimilarity*percent.black,
          data = non.south.dat)
clustered.se = cl(non.south.dat, reg3, non.south.dat$cbsa_NAMELSAD10)
reg3$se = clustered.se

reg4 = lm(obama.vote~dissimilarity*percent.black+dissimilarity*I(percent.black^2),
          data = non.south.dat)
clustered.se = cl(non.south.dat, reg4, non.south.dat$cbsa_NAMELSAD10)
reg4$se = clustered.se

reg5 = lm(obama.vote~dissimilarity+percent.black+income.recode+college.educated,
          data = non.south.dat)
clustered.se = cl(non.south.dat, reg5, non.south.dat$cbsa_NAMELSAD10)
reg5$se = clustered.se

reg6 = lm(obama.vote~dissimilarity+percent.black+I(percent.black^2)+income.recode+college.educated,
          data = non.south.dat)
clustered.se = cl(non.south.dat, reg6, non.south.dat$cbsa_NAMELSAD10)
reg6$se = clustered.se

reg7 = lm(obama.vote~dissimilarity*percent.black+income.recode+college.educated,
          data = non.south.dat)
clustered.se = cl(non.south.dat, reg7, non.south.dat$cbsa_NAMELSAD10)
reg7$se = clustered.se

reg8 = lm(obama.vote~dissimilarity*percent.black+dissimilarity*I(percent.black^2)+income.recode+college.educated,
          data = non.south.dat)
clustered.se = cl(non.south.dat, reg8, non.south.dat$cbsa_NAMELSAD10)
reg8$se = clustered.se

coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     Sweave = T,
                     notes = '',
                     stars = 'default'
)
writeLines(
  outtable, 'TableA10.tex')

